package com.dao.impl;

import com.bean.Teacher;
import com.dao.BaseDao;
import com.utils.SqlUtils;

import java.util.List;
import java.util.Map;

public class TeacherDaoImpl extends BaseDao implements com.dao.TeacherDao {
    @Override
    public List<Teacher> selectForPageItems(int begin, int pageSize, String content) {
        if (content == null) {
            content = "";
        }
        content = "%" + content + "%";
        String sql = SqlUtils.pageSql(Teacher.class, new String[]{"Password"});
        return queryForList(Teacher.class, sql, content, begin, pageSize);
    }

    @Override
    public Integer selectForPageTotalCount(String content) {
        if (content == null) {
            content = "";
        }
        content = "%" + content + "%";
        String sql = SqlUtils.countSql(Teacher.class);
        Number count = (Number) queryForSingleValue(sql, content);
        return count.intValue();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql = "delete from `teacher` where `id` = ?";
        return update(sql, id);
    }

    @Override
    public int insert(Teacher teacher) {
        Map.Entry<String, Object[]> sql = SqlUtils.insertSql(teacher);
        System.out.println(sql.getKey() + ":" + sql.getValue());
        return update(sql.getKey(), sql.getValue());
    }

    @Override
    public Teacher selectBy(Teacher teacher) {
        Map.Entry<String, Object[]> entry = SqlUtils.selectBy(teacher,new String[]{"Password"});
        return queryForOne(Teacher.class, entry.getKey(), entry.getValue());
    }

    @Override
    public int updateByPrimaryKey(Teacher teacher) {
        Map.Entry<String, Object[]> entry = SqlUtils.updateSql(teacher);
        return update(entry.getKey(),entry.getValue());
    }
}
